#import "Building.h"

/**
 * Contains building specific information such as: building tags, 
 * an event list, and for future referece a point of interest list.
 * 
 * @author      Justin Hollander
 * @author      James Lin
 * @version     2.2.4
 * @since       0.1.0
 */
@implementation Building

@synthesize buildingTags;
@synthesize poiList;
@synthesize eventList;
@synthesize buildingImages;
@synthesize secondaryName;
@synthesize mainAbbrev;
@synthesize secondaryAbbrev;
@synthesize description;

/**
 * Initialize the building with given name.
 *
 * @param nm        the name of the building
 * @return          the initialized building with name
 */
- (id)initWithParam:(NSString*)nm {
	if (self == [super init]) {
		self.name = nm;
	}
	
	return self;
}

/**
 * Initialize the building with given information.
 *
 * @param nm        the name of the building
 * @param location  location of the building
 * @param array     the array of building name tags
 * @return          the initialized building with details
 */
- (id)initWithParam:(NSString *)nm 
		gpsLocation:(GPSLocation*)location 
			tagList:(NSMutableArray*)array {
	
	if (self = [self initWithParam:nm]) {
		self.loc = location;
		self.buildingTags = array;	
	}
	
	return self;
}

/**
 * Initialize the building with given information.
 *
 * @param nm        the name of the building
 * @param location  location of the building
 * @param array     the array of building name tags
 * @param mAbbrev   the abbreviation of name of the building
 * @param secName   the secondary name of the building
 * @param sAbbrev   the abbreviation of secondary name of the building
 * @param desc      the description of the building
 * @param images    list of filenames of the building images
 * @return          the initialized building with details
 */
- (id)initWithParam:(NSString *)nm 
		gpsLocation:(GPSLocation*)location 
			tagList:(NSMutableArray*)array 
		 mainAbbrev:(NSString*)mAbbrev 
			secName:(NSString*)secName 
		  secAbbrev:(NSString*)sAbbrev
		description:(NSString*)desc 
			 images:(NSArray*)images {
	
	if (self = [self initWithParam:nm 
					   gpsLocation:location 
						   tagList:array]) {
		self.buildingImages = images;
		self.secondaryName = secName;
		self.mainAbbrev = mAbbrev;
		self.secondaryAbbrev = sAbbrev;
		self.description = desc ? desc : @"";
	}
	
	return self;
}

/**
 * Initializes the building from archive.
 *
 * @param decoder  the NSCoder used for decoding
 * @return         the building decoded from archive
 */
- (id)initWithCoder:(NSCoder*)decoder {
	if (self = [super initWithCoder:decoder]) {
		self.buildingTags = [decoder decodeObjectForKey:@"SFUBuildingTagList"];
		self.poiList = [decoder decodeObjectForKey:@"SFUPoiList"];
		self.eventList = [decoder decodeObjectForKey:@"SFUEventList"];
		self.buildingImages = [decoder decodeObjectForKey:@"SFUBuildingImageList"];
		self.secondaryName = [decoder decodeObjectForKey:@"SFUBuildingSecondaryName"];
		self.mainAbbrev = [decoder decodeObjectForKey:@"SFUBuildingNameAbbrev"];
		self.secondaryAbbrev = [decoder decodeObjectForKey:@"SFUBuildingSecondaryNameAbbrev"];
		self.description = [decoder decodeObjectForKey:@"SFUBuildingDescription"];
		self.description = self.description ? self.description : @"";
	}

	return self;
}

/**
 * Encodes the building for archiving options.
 *
 * @param encoder  the NSCoder used for encoding
 */
- (void)encodeWithCoder:(NSCoder*)encoder {
	if ([encoder allowsKeyedCoding]) {
		[super encodeWithCoder:encoder];
		[encoder encodeObject:self.buildingTags forKey:@"SFUBuildingTagList"];
		[encoder encodeObject:self.poiList forKey:@"SFUPoiList"];
		[encoder encodeObject:self.eventList forKey:@"SFUEventList"];
		[encoder encodeObject:self.buildingImages forKey:@"SFUBuildingImageList"];
		[encoder encodeObject:self.secondaryName forKey:@"SFUBuildingSecondaryName"];
		[encoder encodeObject:self.mainAbbrev forKey:@"SFUBuildingNameAbbrev"];
		[encoder encodeObject:self.secondaryAbbrev forKey:@"SFUBuildingSecondaryNameAbbrev"];
		[encoder encodeObject:self.description forKey:@"SFUBuildingDescription"];
	}
}

/**
 * Compute a unique key for the building.
 *
 * @return  the unique key
 */
- (NSString*)getKey {
	return [super getKey];
}

/**
 * Facilitate alphabetic sorting by building name.
 */
- (NSComparisonResult)compare:(id)otherBuilding {
	return [self.name compare:((Building*)otherBuilding).name];
}

/**
 * Clear its event list.
 */
- (void)flushEventList {
	[self.eventList removeAllObjects];
	[eventList release];
}

/**
 * Deallocate the building.
 */
- (void)dealloc {
	[buildingImages release], buildingImages = nil;
	[secondaryName release], secondaryName = nil;
	[mainAbbrev release], mainAbbrev = nil;
	[secondaryAbbrev release], secondaryAbbrev = nil;
	[description release], description = nil;
	[buildingTags release], buildingTags = nil;
	[poiList release], poiList = nil;
	[eventList release], eventList = nil;
	[super dealloc];
}
@end
